﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using ChuanPlus;
using DevExpress.Xpo;
using MySql.Data.MySqlClient;
namespace ChuanPlus.DAO
{
    class RequestingDAO
    {
        public static void save(Requesting requesting)
        {
            Session.DefaultSession.BeginTransaction();
            try
            {
                Stock office;
                StockCard stCard;
                long idStockCard = StockCardDAO.GenerateId();
                TransactionType tran = TransactionTypeDAO.GetByName("RequestProduct");   
                foreach (RequestingDetails rd in requesting.RequestingDetails)
                {
                    // =========================== CONG THEM VAO OFFICE ================================
                        office = StockDAO.GetStock(rd.StockID.ID.ID,1);
                        office.QtyBalance += rd.Quantity;
                        office.Save();

                    // ========================= TRU SO LUONG SAN PHAM TRONG KHO ========================
                        rd.StockID.QtyBalance -= rd.Quantity;                        
                        rd.StockID.Save();
                        
                    // ================= TAO StockCard CHO TUNG SAN PHAM ================================
                        double Balance = rd.StockID.QtyBalance;
                        stCard = new StockCard(++idStockCard, rd.StockID, requesting.RequestingDate, rd.Quantity, 0, Balance, tran, requesting.RequestingID);
                        stCard.Save();                 
                }
                requesting.Status = "P";
                requesting.Save();
                
                Session.DefaultSession.CommitTransaction();                
                
            }
            catch (Exception e)
            {
                Session.DefaultSession.RollbackTransaction();
                throw new Exception(e.Message);               
            }   
        }
        public static List<Requesting> getReqestings(string status)
        {
            try
            {
                XPQuery<Requesting> requesting = new XPQuery<Requesting>(Session.DefaultSession);
                var list = from r in requesting
                           where r.Status == status
                           select r;
                return new List<Requesting>(list.AsEnumerable<Requesting>());
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
        /// <summary>
        /// GetAllRequesting
        /// </summary>
        /// <returns></returns>
        public static List<Requesting> GetAllRequesting()
        {
            try
            {
                XPQuery<Requesting> requesting = new XPQuery<Requesting>(Session.DefaultSession);
                var list = from r in requesting
                           select r;
                return new List<Requesting>(list.AsEnumerable<Requesting>());
            }
            catch (Exception)
            {
                return null;
            }
        }
        /// <summary>
        /// getRequesting by DAY
        /// </summary>
        /// <param name="d"></param>
        /// <returns></returns>
        public static DataTable GetRequestingByDate(DateTime fromDate, DateTime toDate)
        {
            try
            {
                MySqlParameter[] param = new MySqlParameter[2];
                param[0] = new MySqlParameter("?fromDate", MySqlDbType.DateTime);
                param[0].Value = fromDate;

                param[1] = new MySqlParameter("?toDate", MySqlDbType.DateTime);
                param[1].Value = toDate;

                return DataProvider.execute("getRequestingByDate", param);
            }
            catch (Exception)
            {
                return null;
            }
        }
        /// <summary>
        /// GenerateId
        /// </summary>
        /// <returns></returns>
        public static int GenerateId()
        {
            try
            {
                XPQuery<Requesting> requesting = new XPQuery<Requesting>(Session.DefaultSession);
                var list = (from r in requesting
                            select r).Max(p => p.RequestingID);
                return Int32.Parse(list);
            }
            catch (Exception)
            {
                return 0;
            }
        }
        /// <summary>
        /// GetOrderByID
        /// </summary>
        /// <param name="OrderID"></param>
        /// <returns></returns>
        public static Requesting GetRequestingByID(string requestingID)
        {
            try
            {
                XPQuery<Requesting> requesting = new XPQuery<Requesting>(Session.DefaultSession);
                var list = (from r in requesting
                            where (r.RequestingID.Contains(requestingID))
                            select r).First();
                return list;
            }
            catch (Exception)
            {
                return null;
            }
        }
        /// <summary>
        /// update
        /// </summary>
        /// <param name="order"></param>
        /// <returns></returns>
        public static bool update(Requesting requesting)
        {
            save(requesting);
            return true;
        }
    }
}
